package com.jdk.demo.thread.completableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//跟 thenAccept 方法不一样的是，不关心任务的处理结果。只要上面的任务执行完成，就开始执行 thenRun
public class ThenRunDemo {

	public static void main(String[] args) {
		ExecutorService executorService = Executors.newFixedThreadPool(2);
		CompletableFuture cf = CompletableFuture.supplyAsync(() -> { // 实现了Supplier的get()方法
			try {
				Thread.sleep(2000);
			} catch (Exception e) {
				e.printStackTrace();
			}
			System.out.println("supplyAsync " + Thread.currentThread().getName());
			return 100;
		}, executorService)
		.thenRun(() -> {
	        System.out.println("thenRun ...");
	    });

		System.out.println(Thread.currentThread().getName());
		while (true) {
			if (cf.isDone()) {
				System.out.println("CompletedFuture...isDown " + Thread.currentThread().getName());
				break;
			}
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}
